﻿using ProjectClass.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectClass.View.FrontEnd
{
    public partial class AjaxHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [WebMethod]
        public static string RegisterUser(string email, string password, string displayname)
        {
            ProjectClassEntities db = new ProjectClassEntities();
            MembershipCreateStatus status;
            var user = Membership.CreateUser(email, password,
                email, null, null, false, null, out status);
            if (status == MembershipCreateStatus.Success)
            {
                try
                {
                     var users = db.tbl_Users.Single(m => m.Email.Equals(email));
                     users.DisplayName = displayname;
                     db.Entry(users).State = System.Data.Entity.EntityState.Modified;
                     db.SaveChanges();
                     var authTicket = new FormsAuthenticationTicket(
                                      1,
                                      users.Email,  //user id
                                      DateTime.Now,
                                      DateTime.Now.AddMinutes(30),  // expiry
                                      false,  //true to remember
                                      "", //roles 
                                      "/"
                                    );
                     SimpleSessionPersister.UserName = users.Email;
                     FormsAuthentication.SetAuthCookie(users.Email, false /* createPersistentCookie */);
                     return "Success";
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
            else if (status == MembershipCreateStatus.DuplicateUserName)
            {
                return "Duplicate";
            }
            return "Failed";
        }

        [WebMethod]
        public static string LoginUser(string email, string password, bool remember)
        {
            try
            {
                if (Membership.ValidateUser(email, password))
                {
                    if (remember)
                    {
                        const int timeout = 43200; // Timeout is in minutes, 525600 = 365 days; 1 day = 1440.
                        var ticket = new FormsAuthenticationTicket(email, remember, timeout);
                        SimpleSessionPersister.UserName = email;
                        //ticket.
                        string encrypted = FormsAuthentication.Encrypt(ticket);
                        var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted)
                        {
                            Expires = DateTime.Now.AddMinutes(timeout),
                            HttpOnly = true
                        };
                        HttpContext.Current.Response.Cookies.Add(cookie);

                    }
                    else
                    {
                        var ticket = new FormsAuthenticationTicket(
                                     1,
                                     email,  //user id
                                     DateTime.Now,
                                     DateTime.Now.AddMinutes(30),  // expiry
                                     true,  //true to remember
                                     "", //roles 
                                     "/"
                                   );
                        SimpleSessionPersister.UserName = email;
                        FormsAuthentication.SetAuthCookie(email, false /* createPersistentCookie */);
                    }
                    return "Success";
                }
                return "Failed";
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        [WebMethod]
        public static string LogOutUser()
        {
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Session.Clear();
            FormsAuthentication.SignOut();
            return string.Empty;
        }
    }
}